/**
 * 
 */

function Drawer()
{
	/**
	 * 
	 */
	this.cache = 0;
	
	this.timer = new Timer();
	
	
	/**
	 *
	 */
	this.drawPoint = function (index, i, canvasType)
	{	
		/**
		 * Color and others styles should be initialised before this function is called !
		 */
		//canvas[canvasType+1].setLineDash([15]);
		
		canvas[canvasType+1].strokeStyle = shape_manager.data[index].color.toString();
		canvas[canvasType+1].lineWidth = shape_manager.data[index].thickness;
		canvas[canvasType+1].lineJoin = "round";
		
		canvas[canvasType+1].beginPath();
		canvas[canvasType+1].moveTo(shape_manager.data[index].x[i] , shape_manager.data[index].y[i]);
		canvas[canvasType+1].lineTo(shape_manager.data[index].x[i+1], shape_manager.data[index].y[i+1]);
		
		canvas[canvasType+1].closePath();
		canvas[canvasType+1].stroke();
	};
	
	
	/**
	 */
	this.drawShape = function(index, canvasType)
	{
		/*canvas[canvasType+1].strokeStyle = shape_manager.data[index].color.toString();
		canvas[canvasType+1].lineWidth = shape_manager.data[index].width;
		canvas[canvasType+1].lineJoin = "round";*/
		
		
		
		//canvas[canvasType+1].moveTo(shape_manager.data[index].x[this.cache], shape_manager.data[index].y[this.cache]);
		//this.cache++;
		//canvas[canvasType+1].beginPath();
		if (interfaces.current_shape == "Free")
		{
			for (var i = this.cache ; i < shape_manager.data[index].x.length ; i++)
			{
				this.drawPoint(index, i, canvasType);
			}
			this.cache = shape_manager.data[index].x.length - 1;
		}
		else
		{
			for (var i = 0 ; i < shape_manager.data[index].x.length ; i++)
			{
				this.drawPoint(index, i, canvasType);
			}
		}
		//canvas[canvasType+1].stroke();
		//canvas[canvasType+1].closePath();
		
		
	};
	
	this.drawShapeOpt = function(index)
	{
	//console.log("realx = " + shape_manager.data[index].real_x + "    realy=  "  + shape_manager.data[index].real_y);
		if (index != null){
			if (this.transform){
				canvas[STATIC_CONTEXT].save();
			
				/* Translating to the center of the canvas */
				canvas[STATIC_CONTEXT].translate(shape_manager.data[index].real_x, shape_manager.data[index].real_y );
				canvas[STATIC_CONTEXT].scale(shape_manager.data[index].scale_x, shape_manager.data[index].scale_y);		
				canvas[STATIC_CONTEXT].translate(-shape_manager.data[index].real_x, -shape_manager.data[index].real_y );
			}
			
			canvas[STATIC_CONTEXT].drawImage(shape_manager.data[index].cacheCanvas,
					shape_manager.data[index].real_x,
					shape_manager.data[index].real_y);
			}
			
			if (this.transform){
				canvas[STATIC_CONTEXT].restore();
				}
		
	};
	
	this.drawCanvasOpt = function()
	{
		redrawer.stop();
		
		this.clearCanvas();


		for (var i = 0 ; i < shape_manager.data.length ; i++)
		{
			
			/*canvas[STATIC_CONTEXT].drawImage(shape_manager.data[i].cacheCanvas,
					shape_manager.data[i].real_x,
					shape_manager.data[i].real_y);*/
					
				this.drawShapeOpt(i);
		}

	};
	
	/**
	 * Erases the content of both canvas
	 */
	this.clearCanvas = function()
	{
		redrawer.stop();
		canvas[STATIC_CANVAS].width = canvas[STATIC_CANVAS].width;
		canvas[DYNAMIC_CANVAS].width = canvas[DYNAMIC_CANVAS].width;
	};
	
	/**
	 * Used for the current drawing shape and nothing other
	 */
	this.refreshCanvas = function(index)
	{
		if (interfaces.current_shape != "Free")
			canvas[DYNAMIC_CANVAS].width = canvas[DYNAMIC_CANVAS].width;
		
		this.drawShape(index, DYNAMIC_CANVAS);
	};
	
	/**
	 * Once we finished the current drawing we call this method
	 */
	this.updateCanvases = function()
	{
		// Clear the dynamic canvas
		canvas[DYNAMIC_CANVAS].width = canvas[DYNAMIC_CANVAS].width;

		//canvas[STATIC_CONTEXT].save();
		//canvas[STATIC_CONTEXT].scale(2, 2);
		
		this.drawShapeOpt(shape_manager.lastIndex());
		
	/*	canvas[STATIC_CONTEXT].drawImage(shape_manager.data[shape_manager.lastIndex()].cacheCanvas,
				shape_manager.data[shape_manager.lastIndex()].real_x,
				shape_manager.data[shape_manager.lastIndex()].real_y);*/
		
		//canvas[STATIC_CONTEXT].restore();
	};
	
	this.drawCanvasExcept = function(index)
	{
		
		redrawer.stop();
		
		this.clearCanvas();


		for (var i = 0 ; i < shape_manager.data.length ; i++)
		{
			if (i != index)
			{
				/*canvas[STATIC_CONTEXT].drawImage(shape_manager.data[i].cacheCanvas,
						shape_manager.data[i].real_x,
						shape_manager.data[i].real_y);*/
				this.drawShapeOpt(i);
			}
			
		}
	};
	
	
	
	this.setCursorStyle = function(style)
	{
		//console.log("style number ----> " + style);
		switch (style){
			case 1:
				canvas[0].style.cursor = "nw-resize";
				canvas[2].style.cursor = "nw-resize";
				break;
			case 2:
				canvas[0].style.cursor = "sw-resize";
				canvas[2].style.cursor = "sw-resize";
				break;
			case 3:
				canvas[0].style.cursor = "ne-resize";
				canvas[2].style.cursor = "ne-resize";
				break;
			case 4:
				canvas[0].style.cursor = "se-resize";
				canvas[2].style.cursor = "se-resize";
				break;
			case 5:
				canvas[0].style.cursor = "all-scroll";
				canvas[2].style.cursor = "all-scroll";
				break;
			case 6:
				canvas[0].style.cursor = "default";
				canvas[2].style.cursor = "default";
				break;
			default:
				canvas[0].style.cursor = "default";
				canvas[2].style.cursor = "default";
				break;
		}
		canvas[0].style.cursor = style;
		canvas[2].style.cursor = style;
	};
	
	this.setScale = function(x, y, ctx){
		//console.log("OOKOKOKOKO");
		
		this.transform = true;
		this.scale_x = x;
		this.scale_y = y;
		
	}
	
	
	this.drawCanvas = function()
	{
		redrawer.stop();
		
		this.clearCanvas();
		
		this.cache = 0;
		
		for (var i = 0 ; i < shape_manager.data.length ; i++)
		{
			this.drawShape(i, STATIC_CANVAS);
			this.cache = 0;
		}
	};
}

